package com.dwin.dingnuo.ui;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;


import com.dwin.dingnuo.R;
import com.dwin.dingnuo.http.response.HomeDevice;

import java.util.ArrayList;
import java.util.List;

/**
 * 首页，设备列表适配器
 */
public class DeviceAdapter extends BaseAdapter {
    private Context mContext = null; // 上下文对象
    private List<HomeDevice> mData = new ArrayList<>();
    private LayoutInflater inflater = null;

    public DeviceAdapter(Context context, List<HomeDevice> data) {
        this.mContext = context;
        this.mData = data;
        this.inflater = LayoutInflater.from(context);
    }

    public void setData(List<HomeDevice> mData) {
        this.mData = mData;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public Object getItem(int position) {
        return mData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.item_device, parent, false); //加载布局
            holder.rlBodyBg = convertView.findViewById(R.id.rlBodyBg);
            holder.tv_device_name = convertView.findViewById(R.id.tv_device_name);
            holder.tv_device_status = convertView.findViewById(R.id.tv_device_status);
            holder.iv_device_delete = convertView.findViewById(R.id.iv_device_delete);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        HomeDevice device = mData.get(position); // get item
        if (device.devices_members_info!=null && device.devices_members_info.name!=null) {
            holder.tv_device_name.setText(device.devices_members_info.name);
        } else {
            holder.tv_device_name.setText(device.device_num); // 设备编号。
        }
        holder.iv_device_delete.setVisibility(isShowDelete ? View.VISIBLE : View.GONE);// 设置删除按钮是否显示
        if (device.device_alive_or_dead == 1) { // 设备的在线状态（0离线，1在线）
            holder.tv_device_status.setText("在线");
            holder.rlBodyBg.setBackgroundResource(R.drawable.device_on_bg);
        } else {
            holder.tv_device_status.setText("离线");
            holder.rlBodyBg.setBackgroundResource(R.drawable.device_off_bg);
        }
        holder.iv_device_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (adapterListener != null) {
                    adapterListener.onClkDelete(position);
                }
            }
        });
        return convertView;
    }

    public class ViewHolder {
        public RelativeLayout rlBodyBg;
        public TextView tv_device_name;
        public TextView tv_device_status;
        public ImageView iv_device_delete;
    }

    private boolean isShowDelete = false; // 是否显示删除键

    public void setIsShowDelete(boolean isShowDelete) {
        this.isShowDelete = isShowDelete;
        notifyDataSetChanged();
    }

    public OnAdapterListener adapterListener = null;

    public void setOnAdapterListener(OnAdapterListener adapterListener) {
        this.adapterListener = adapterListener;
    }

    public interface OnAdapterListener {
        void onClkDelete(int position);
    }

}
